package com.example.flight.controller;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.example.flight.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import com.example.flight.entity.FlightInformationEntity;
import com.example.flight.service.FlightInformationService;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author hws
 * @email 231884142@qq.com
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/flights")
@CrossOrigin
public class FlightInformationController {
    @Autowired
    private FlightInformationService flightInformationService;

    /**
     * 列表
     */
    @GetMapping
    public Result list(String departure, String destination, Timestamp departureDate , HttpServletRequest request){//2023-06-12 00:00:00//其他格式可以用 @DateTimeFormat() Date
        System.out.println(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));

        return Result.ok().data("list",flightInformationService.searchList(departure,destination,departureDate) );
    }
    /**
     * 列表
     */
    @GetMapping("/admin")
    public Result listForAdmin(String flightNum,String departure, String destination, Timestamp departureDate , HttpServletRequest request){//2023-06-12 00:00:00//其他格式可以用 @DateTimeFormat() Date


        return Result.ok().data("list",flightInformationService.searchList2(flightNum,departure,destination,departureDate) );
    }


    /**
     * 信息
     */
    @GetMapping("/{fid}")
    public Result info(@PathVariable("fid") String fid, HttpServletRequest request){
        System.out.println(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
		FlightInformationEntity flightInformation = flightInformationService.getById(fid);

        return Result.ok().data("flightInformation", flightInformation);
    }

    /**
     * 保存
     */
    @PostMapping
    public Result save(@RequestBody FlightInformationEntity flightInformation){
        flightInformation.setEconomyClassAvailable(flightInformation.getEconomyClassSeats());
        flightInformation.setBusinessClassAvailable(flightInformation.getBusinessClassSeats());
		flightInformationService.save(flightInformation);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public Result update(@RequestBody FlightInformationEntity flightInformation){
		flightInformationService.updateById(flightInformation);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{fid}")
    public Result delete(@PathVariable("fid") String fid, HttpServletRequest request){
        System.out.println(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
        boolean b = flightInformationService.removeById(fid);
        if (b)  return Result.ok();
      return Result.error();
    }

}
